#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

pthread_mutex_t mx= PTHREAD_MUTEX_INITIALIZER;
static int g_count= 0;

void *read_write_mem(void * _val)
{
	int val = 0;
	int i = 0;

	for(;i < 5000;++i)
	{
		pthread_mutex_lock(&mx);
		val = g_count;
		printf("pthread idis:%ld, count is:%d \n",pthread_self(),g_count);
		g_count = val+1;
		pthread_mutex_unlock(&mx);
	}

	return  NULL;
}
 
int main()
{
	pthread_t tid1;
	pthread_t tid2;
	pthread_t tid3;
	pthread_t tid4;

	pthread_create(&tid1,NULL,read_write_mem,NULL);
	pthread_create(&tid2,NULL,read_write_mem,NULL);
	pthread_create(&tid3,NULL,read_write_mem,NULL);
	pthread_create(&tid4,NULL,read_write_mem,NULL);

	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	pthread_join(tid3,NULL);
	pthread_join(tid4,NULL);

	printf("count val is%d\n",g_count);

	return 0;
}


